// pages/Conveinfo/content/content.js
var app = getApp();
var ikz = require('../../Common/tools.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    conven_info: '',//本条信息
    page: 1,//页数
    limit: 6,//每页条数
    conven_top_info: '',//优选信息
    cid: '',//本条消息id
    iscollect: 'wei',//本条消息是否赞过
    prodis: 'none',//底部提示是否显示
    iscol: 2,//2可以操作收藏，3不可以，防止重复点击
    imgnum: 1,//banner第几张
    isChecked:false,


    // / 图片  
     hdimg: [],
    //是否采用衔接滑动  
    circular: true,
    //是否显示画板指示点  
    indicatorDots: false,
    //选中点的颜色  
    indicatorcolor: "#000",
    //是否竖直  
    vertical: false,
    //是否自动切换  
    autoplay: false,
    //滑动动画时长毫秒  
    duration: 100,
    //所有图片的高度  
    imgheights: [],
    //图片宽度  
    imgwidth: 750,
    //默认  
    current: 0  
  },
  

  imageLoad: function (e) {
    //获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    console.log(imgwidth, imgheight)
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    //把每一张图片的高度记录到数组里  
    imgheights.push(imgheight)
    this.setData({
      imgheights: imgheights,
    })
  },  


  //banner变化
  swiperChange: function (e) {
    var imgnum = e.detail.current + 1;
    this.setData({
      imgnum: imgnum,
    });
  },
  
  //跳转到图片页
  toImg: function (e) {
    var id = e.currentTarget.dataset.id;
    var typ = 'bian';
    var data = {
      id: id,
      typ: typ,
    }
    ikz.nav('../../Home/images/images', 'n', data);
  },
  //拨打电话
  toPhone: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  serviceSelection:function(){
    console.log(123)
    this.setData({
      isChecked: true
    })  
  },
  //跳转到详情页
  toContent: function (e) {
    var cid = e.currentTarget.dataset.cid;
    var data = {
      cid: cid,
    }
    ikz.nav('../content/content', 'n', data);
  },
  //本条消息收藏
  collectConven: function (e) {
    var cid = this.data.cid;
    var that = this
    var data = {
      cid: cid,
    };
    that.setData({
      iscol: 3,
    });
    var path = 'Conven/collectConven';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '收藏成功！',
          duration: 2000
        });
        that.setData({
          iscollect: 'yi',
        });
      } else if (res.data.status == 'fails') {
        wx.showToast({
          title: '已收藏！',
          duration: 2000
        });
      }else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  //本条消息取消收藏
  haveCollect: function (e) {
    var cid = this.data.cid;
    var that = this
    var data = {
      cid: cid,
    };
    that.setData({
      iscol: 3,
    });
    var path = 'Conven/collectCancle';
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        wx.showToast({
          title: '取消收藏成功！',
          duration: 2000
        });
        that.setData({
          iscollect: 'wei',
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        });
      }
      that.setData({
        iscol: 2,
      });
    });
  },
  
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var cid = options.cid;
    var that = this;
    ikz.over(that);
    that.setData({
      cid: cid,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext = wx.createVideoContext('myVideo');
    this.videoContext.requestFullScreen();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var cid = this.data.cid;
    var page = 1;
    var limit = this.data.limit;
    var that = this;
    var data = {
      cid: cid,
      page: page,
      limit: limit,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    var path = 'Conven/content';
    ikz.request(path, data, islogin, (res) => {
      console.log(res);
      console.log(8888);
      if (res.data.status == 'success') {
        var conven_info = res.data.conven_info;
        if (conven_info.conven_red_ismature == 2 && conven_info.conven_red_id > 0) {
          var noticedis = 'block';
        } else {
          var noticedis = 'none';
        }
        that.setData({
          conven_info: res.data.conven_info,
          conven_top_info: res.data.conven_top_info,
          noticedis: noticedis,
          cid: cid,
          iscollect: res.data.iscollect,
          page: page,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '刷新中..',
    })
    var cid = this.data.cid;
    var page = 1;
    var limit = this.data.limit;
    var that = this;
    var data = {
      cid: cid,
      page: page,
      limit: limit,
    }
    if (app.globalData.openUserInfo == '') {
      var islogin = false;
    } else {
      var islogin = true;
    }
    var path = 'Conven/content';
    ikz.request(path, data, islogin, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var conven_info = res.data.conven_info;
        if (conven_info.conven_red_ismature == 2 && conven_info.conven_red_id > 0) {
          var noticedis = 'block';
        } else {
          var noticedis = 'none';
        }
        that.setData({
          conven_info: res.data.conven_info,
          conven_top_info: res.data.conven_top_info,
          noticedis: noticedis,
          cid: cid,
          iscollect: res.data.iscollect,
          page: page,
        });
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '加载中',
    });
    var that = this;
    var page = this.data.page + 1;
    var old_info = this.data.conven_top_info;
    this.setData({
      page: page,
    })
    var limit = this.data.limit;
    var data = {
      page: page,
      limit: limit,
    }
    var path = 'Conven/content';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var new_info = res.data.conven_top_info;
        if(new_info.length){
          var all_info = old_info.concat(new_info);
          var prodis = 'none';
        }else{
          var all_info = old_info;
          var prodis = 'block';
        }
        
        that.setData({
          conven_top_info: all_info,
          prodis: prodis,
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getRandomColor() {
    let rgb = []
    for (let i = 0; i < 3; ++i) {
      let color = Math.floor(Math.random() * 256).toString(16)
      color = color.length == 1 ? '0' + color : color
      rgb.push(color)
    }
    return '#' + rgb.join('')
  },
  inputValue: '',
  bindInputBlur: function (e) {
    this.inputValue = e.detail.value
  },
  bindSendDanmu: function () {
    console.log(this.getRandomColor())
    this.videoContext.sendDanmu({
      text: this.inputValue,
      color: this.getRandomColor()
    })
  },
})